package com.cskaoyan.service.impl;

import com.cskaoyan.bean.User;
import com.cskaoyan.bean.UserData;
import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.bean.pojo.*;
import com.cskaoyan.bean.vo.*;


import com.cskaoyan.bean.vo.UserAddressListVO;


import com.cskaoyan.mapper.*;
import com.cskaoyan.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author stone
 * @date 2022/01/06 16:36
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    @Autowired
    MarketUserMapper marketUserMapper;

    @Autowired
    MarketGoodsMapper marketGoodsMapper;

    @Autowired
    MarketGoodsProductMapper marketGoodsProductMapper;

    //
    @Autowired
    MarketOrderMapper marketOrderMapper;

    // 用户地址
    @Autowired
    MarketAddressMapper marketAddressMapper;

    // 用户收藏的变量
    @Autowired
    MarketCollectMapper marketCollectMapper;


    // 用户意见反馈的变量
    @Autowired
    MarketFeedbackMapper marketFeedbackMapper;


    // 会员足迹
    @Autowired
    MarketFootprintMapper marketFootprintMapper;

    // 用户搜索历史
    @Autowired
    MarketSearchHistoryMapper marketSearchHistoryMapper;



    @Override
    public UserData query(BaseParam param, String username) {
        Integer page = param.getPage();//页码
        Integer limit = param.getLimit();//数据量
        String sort = param.getSort();//排序列
        String order = param.getOrder();//desc\asc
        //分页插件 PageHelper，辅助我们做分页以及分页信息的获得
        // 开启分页
        PageHelper.startPage(page, limit);
        //执行查询过程中拼接分页信息
        List<User> list = userMapper.select(sort,order,username);

        //total、pages、list
        //通过PageHelper帮我们来获得一些和分页相关的信息PageInfo
        //使用其有参构造方法 → 形参写来源于数据库的list
        PageInfo<User> pageInfo = new PageInfo<>(list);
        // list.size() 不一定等于 != total  --> total >= list.size()
        long total = pageInfo.getTotal();// 拿到满足条件的总的数据量 (不考虑分页)
        int pages = pageInfo.getPages();
        return UserData.data(total,pages,limit,page,list); // 数据的封装
    }

    // 首页
    @Override
    public DashboardVO dashboard() {

        MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
        long goodsTotal = marketGoodsMapper.countByExample(marketGoodsExample);

        MarketUserExample example = new MarketUserExample();
        long userTotal = marketUserMapper.countByExample(example);

        MarketGoodsProductExample marketGoodsProductExample = new MarketGoodsProductExample();
        long productTotal = marketGoodsProductMapper.countByExample(marketGoodsProductExample);

        MarketOrderExample marketOrderExample = new MarketOrderExample();
        long orderTotal = marketOrderMapper.countByExample(marketOrderExample);

        DashboardVO dashboardVO = new DashboardVO();
        dashboardVO.setGoodsTotal((int) goodsTotal);
        dashboardVO.setUserTotal((int) userTotal);
        dashboardVO.setProductTotal((int) productTotal);
        dashboardVO.setOrderTotal((int) orderTotal);

        return dashboardVO;
    }

    // 用户管理-收货地址
    @Override
    public UserAddressListVO queryAddressList(BaseParam param, String name, Integer userId) {
        // 获取分页相关信息
        Integer page = param.getPage();
        Integer limit = param.getLimit();
        String sort = param.getSort();
        String order = param.getOrder();
        // 开启分页
        PageHelper.startPage(page, limit);

        // 拼接排序依据：sort和order
        MarketAddressExample addressExample = new MarketAddressExample();
        addressExample.setOrderByClause(sort + " " + order);
        // 如果参数有用户名和用户id，加上查询条件
        MarketAddressExample.Criteria criteria = addressExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (name != null && !("".equals(name))) {
            criteria.andNameLike("%" + name + "%");
        }
        if (userId != null && userId != 0) {
            criteria.andUserIdEqualTo(userId);
        }
        // 执行查询
        List<MarketAddress> addressList = marketAddressMapper.selectByExample(addressExample);

        // 获取查询结果的分页相关信息
        PageInfo<MarketAddress> pageInfo = new PageInfo<>(addressList);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();

        return new UserAddressListVO(total, pages, limit, page, addressList);
    }

    @Override
    public UserListVO userList(BaseParam baseParam,String username,String mobile) {
        MarketUserExample marketUserExample = new MarketUserExample();
        MarketUserExample.Criteria criteria = marketUserExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        if(username!=null && !"".equals(username)){
            criteria.andUsernameLike("%"+username+"%");
        }
        if(mobile!=null && !"".equals(mobile)){
            criteria.andMobileEqualTo(mobile);
        }
        marketUserExample.setOrderByClause(baseParam.getSort() + " " + baseParam.getOrder());
        PageHelper.startPage(baseParam.getPage(), baseParam.getLimit());
        List<MarketUser> marketUsers = marketUserMapper.selectByExample(marketUserExample);
        PageInfo<MarketUser> pageInfo = new PageInfo<>(marketUsers);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        return UserListVO.data(total, pages, baseParam.getPage(), baseParam.getLimit(), marketUsers);
    }

    @Override
    public MarketUser userDetail(Integer id) {
        return marketUserMapper.selectByPrimaryKey(id);
    }

    // 会员收藏
    @Override
    public CollectListVO collectList(Integer page, Integer limit, String sort, String order, Integer userId, Integer valueId) {
        CollectListVO collectListVO = new CollectListVO();

        // 开启分页
        PageHelper.startPage(page, limit);

        // 获取收藏列表--> marker_collect表
        MarketCollectExample marketCollectExample = new MarketCollectExample();
        MarketCollectExample.Criteria criteria = marketCollectExample.createCriteria();
        criteria.andAddTimeIsNotNull();
        criteria.andDeletedEqualTo(false);
        marketCollectExample.setOrderByClause(sort +" "+ order);
        if (userId != null && !"".equals(userId)) {
            criteria.andUserIdEqualTo(userId);
        }
        if (valueId != null && !"".equals(valueId)) {
            criteria.andValueIdEqualTo(valueId);
        }
        List<MarketCollect> marketCollects = marketCollectMapper.selectByExample(marketCollectExample);

        // 获取查询结果的分页相关信息
        PageInfo<MarketAddress> pageInfo = new PageInfo<>();
        long total = pageInfo.getTotal();
        Integer pages = pageInfo.getPages();

        // 返回
        return new CollectListVO((int) total,pages,limit,page,marketCollects);
    }

    // todo picUrls 需要返回的是一个字符串数组,现在返回的是一个String
    @Override
    public FeedbackListVO feedbackList(Integer page, Integer limit, String username, String order, String sort, Integer id) {
        FeedbackListVO feedbackListVO = new FeedbackListVO();

        // 开启分页
        PageHelper.startPage(page, limit);

        // 获取意见反馈--> market_feedback表
        MarketFeedbackExample marketFeedbackExample = new MarketFeedbackExample();
        MarketFeedbackExample.Criteria criteria = marketFeedbackExample.createCriteria();
        criteria.andAddTimeIsNotNull();
        criteria.andDeletedEqualTo(false);
        marketFeedbackExample.setOrderByClause(sort +" "+ order);
        // 实现条件查询
        if (username != null && !"".equals(username)) {
            criteria.andUsernameLike(username);
        }
        if (id != null && !"".equals(id)) {
            criteria.andIdEqualTo(id);
        }
        List<MarketFeedback> marketCollects = marketFeedbackMapper.selectByExample(marketFeedbackExample);

        feedbackListVO.setList(marketCollects);

        // 获取查询结果的分页相关信息
        PageInfo<MarketAddress> pageInfo = new PageInfo<>();
        long total = pageInfo.getTotal();
        Integer pages = pageInfo.getPages();

        return new FeedbackListVO((int) total,pages,limit,page,marketCollects);
    }

    @Override
    public FootprintListVO footprintList(BaseParam baseParam, Integer userId, Integer goodsId) {
        MarketFootprintExample marketFootprintExample = new MarketFootprintExample();

        MarketFootprintExample.Criteria criteria = marketFootprintExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (userId != null) {
            criteria.andIdEqualTo(userId);
        }
        if (goodsId != null) {
            criteria.andGoodsIdEqualTo(goodsId);
        }
        marketFootprintExample.setOrderByClause(baseParam.getSort() + " " + baseParam.getOrder());
        PageHelper.startPage(baseParam.getPage(), baseParam.getLimit());
        List<MarketFootprint> marketFootprints = marketFootprintMapper.selectByExample(marketFootprintExample);
        PageInfo<MarketFootprint> pageInfo = new PageInfo<>(marketFootprints);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        return FootprintListVO.data(total, pages, baseParam.getLimit(), baseParam.getLimit(), marketFootprints);
    }

    @Override
    public int updateUser(MarketUser marketUser) {
        marketUser.setAddTime(new Date());
        marketUser.setUpdateTime(new Date());

        int count = marketUserMapper.updateByPrimaryKeySelective(marketUser);
        return count;
    }

    // 用户管理-搜索历史 用户管理-搜索历史：market_search_history表
    @Override
    public UserHistoryListVO queryHistoryList(BaseParam param, String keyword, Integer userId) {
        // 获取分页相关信息
        Integer page = param.getPage();
        Integer limit = param.getLimit();
        String sort = param.getSort();
        String order = param.getOrder();
        // 开启分页
        PageHelper.startPage(page, limit);

        MarketSearchHistoryExample historyExample = new MarketSearchHistoryExample();
        historyExample.setOrderByClause(sort + " " + order);
        MarketSearchHistoryExample.Criteria criteria = historyExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (keyword != null && !("".equals(keyword))) {
            criteria.andKeywordLike("%" + keyword + "%");
        }
        if (userId != null && userId != 0) {
            criteria.andUserIdEqualTo(userId);
        }
        List<MarketSearchHistory> historyList = marketSearchHistoryMapper.selectByExample(historyExample);

        // 获取分页信息
        PageInfo<MarketSearchHistory> pageInfo = new PageInfo<>(historyList);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();

        return new UserHistoryListVO(total, pages, limit, page, historyList);

    }
}
